package com.wkhyapp.lm.view;

import android.content.Intent;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lidong.photopicker.PhotoPickerActivity;
import com.lidong.photopicker.SelectModel;
import com.lidong.photopicker.intent.PhotoPickerIntent;
import com.wkhyapp.lm.R;
import com.wkhyapp.lm.base.SuperActivity;
import com.wkhyapp.lm.contract.UpLoadPresenter;
import com.wkhyapp.lm.contract.UploadView;
import com.wkhyapp.lm.http.vo.Category;
import com.wkhyapp.lm.http.vo.Goods;
import com.wkhyapp.lm.utils.MemberUtils;
import com.wkhyapp.lm.utils.StatusBarFontUtil;
import com.wkhyapp.lm.weigit.dialog.CategoryDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class EditorGoodsActivity_bak extends SuperActivity<UpLoadPresenter> implements UploadView {

    private static final int REQUEST_CAMERA_CODE = 10;
    private static final int REQUEST_PREVIEW_CODE = 20;
    private ArrayList<String> imagePaths = new ArrayList<>();
    private ArrayList<String> imgList = new ArrayList<>();
    private Goods goods;
    GridAdapter gridAdapter;
    @BindView(R.id.back_iv)
    ImageView back_iv;
    @BindView(R.id.et_context)
    EditText et_context;
    @BindView(R.id.fl_tv)
    TextView fl_tv;
    @BindView(R.id.price_et)
    EditText price_et;
    int fenlei = 0;
    @BindView(R.id.gridView_gv)
    GridView gridView;
    CategoryDialog categoryDialog;
    @BindView(R.id.category_rl)
    RelativeLayout category_rl;
    private List<Category> categories = new ArrayList<>();
    @BindView(R.id.find_comment_submit)
    TextView tv_click;
    @Override
    public void initUI() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            translucentStatusBar();
        }
        StatusBarFontUtil.setLightStatusBarColor(this);
        goods = (Goods) getIntent().getSerializableExtra("0");
        et_context.setText(goods.getDesc());
        fl_tv.setText(goods.getCategoryId()+"");
        if(goods.getCategoryId()!=null){
            fl_tv.setText(goods.getCategoryId()+"");
        }else{
            fl_tv.setText("全部");
            fenlei  = 0;
        }

        //fenlei = goods.getCategoryId();
        if(goods.getPrice()!=null){
            price_et.setText(goods.getPrice().toString());
        }else{
            price_et.setText("0.00");
        }

        int cols = getResources().getDisplayMetrics().widthPixels / getResources().getDisplayMetrics().densityDpi;
        cols = cols < 3 ? 3 : cols;
        gridView.setNumColumns(cols);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String imgs = (String) parent.getItemAtPosition(position);
                if ("paizhao".equals(imgs)) {
                    PhotoPickerIntent intent = new PhotoPickerIntent(EditorGoodsActivity_bak.this);
                    intent.setSelectModel(SelectModel.MULTI);
                    intent.setShowCarema(true); // 是否显示拍照
                    intent.setMaxTotal(10-imagePaths.size()); // 最多选择照片数量，默认为6
                    intent.setSelectedPaths(imagePaths); // 已选中的照片地址， 用于回显选中状态
                    startActivityForResult(intent, REQUEST_CAMERA_CODE);
                }
            }
        });
        imagePaths.addAll(goods.getGoodsImgs());
        if(imagePaths.size()<9){
            imagePaths.add("paizhao");
        }

        gridAdapter = new GridAdapter(imagePaths);
        gridView.setAdapter(gridAdapter);
        gridAdapter.notifyDataSetChanged();
        mPresenter.getCategry(MemberUtils.getUid());
    }

    @Override
    protected void initListener() {
        back_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        gridAdapter.setCallBack(new callBack() {
            @Override
            public void action(int position) {
                imagePaths.remove(position);
                if(imagePaths.size()<9){
                    if(!imagePaths.contains("paizhao")){
                        imagePaths.add("paizhao");
                    }

                }
                gridAdapter.notifyDataSetChanged();
            }
        });

        category_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                categoryDialog.show();
            }
        });

        tv_click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TToast("暂未实现");
            }
        });
    }





    @Override
    public void setToken(String toekn) {

    }

    @Override
    public void upLoadSucc() {

    }

    @Override
    public void setCategoory(List<Category> list) {
        Category category = new Category();
        category.setName("全部");
        category.setId(0);
        list.add(0,category);
        categories.addAll(list);
        categoryDialog = new CategoryDialog(mContext,categories);
        categoryDialog.init();
        categoryDialog.setCallBack(new CategoryDialog.callBack() {
            @Override
            public void jump(Category item) {
                fenlei = item.getId();
                fl_tv.setText(item.getName());
            }
        });
    }

    @Override
    protected UpLoadPresenter createPresenter() {
        return mPresenter = new UpLoadPresenter(this);
    }


    @Override
    protected void afterCreate() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_editor_goods;
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                // 选择照片
                case REQUEST_CAMERA_CODE:
                    ArrayList<String> list = data.getStringArrayListExtra(PhotoPickerActivity.EXTRA_RESULT);
                    Log.d(TAG, "数量：" + list.size());
                    loadAdpater(list);
                    break;
            }
        }
    }


    private void loadAdpater(ArrayList<String> paths) {
//        if (imagePaths != null && imagePaths.size() > 0) {
//            imagePaths.clear();
//        }
        imagePaths.remove("paizhao");
        imagePaths.addAll(paths);
        if(imagePaths.size()<9){
            imagePaths.add("paizhao");
        }
        gridAdapter = new GridAdapter(imagePaths);
        gridView.setAdapter(gridAdapter);
        gridAdapter.notifyDataSetChanged();
    }


    private void uploadAdpater(ArrayList<String> paths) {
        if (imagePaths != null && imagePaths.size() > 0) {
            imagePaths.clear();
        }
        imagePaths.addAll(paths);
        if(imagePaths.size()<9){
            imagePaths.add("paizhao");
        }
        gridAdapter = new GridAdapter(imagePaths);
        gridView.setAdapter(gridAdapter);
        gridAdapter.notifyDataSetChanged();
    }


    private class GridAdapter extends BaseAdapter {
        private ArrayList<String> listUrls;
        private LayoutInflater inflater;
        public  callBack mCallBack;
        public GridAdapter(ArrayList<String> listUrls) {
            this.listUrls = listUrls;
            if (listUrls.size() == 7) {
                listUrls.remove(listUrls.size() - 1);
            }
            inflater = LayoutInflater.from(EditorGoodsActivity_bak.this);
        }

        public int getCount() {
            return listUrls.size();
        }

        @Override
        public String getItem(int position) {
            return listUrls.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            EditorGoodsActivity_bak.GridAdapter.ViewHolder holder = null;
            if (convertView == null) {
                holder = new GridAdapter.ViewHolder();
                convertView = inflater.inflate(R.layout.item_xc, parent, false);
                holder.image = (ImageView) convertView.findViewById(R.id.imageView);
                holder.delIv = (ImageView) convertView.findViewById(R.id.delete_iv);
                convertView.setTag(holder);
            } else {
                holder = (EditorGoodsActivity_bak.GridAdapter.ViewHolder) convertView.getTag();
            }

            final String path = listUrls.get(position);
            if (path.equals("paizhao")) {
                holder.delIv.setVisibility(View.GONE);
                holder.image.setImageResource(R.drawable.icon_add);
            } else {
                holder.delIv.setVisibility(View.VISIBLE);
                Glide.with(EditorGoodsActivity_bak.this)
                        .load(path)
                        .placeholder(R.drawable.default_error)
                        .error(R.drawable.default_error)
                        .centerCrop()
                        .crossFade()
                        .into(holder.image);
                holder.delIv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mCallBack.action(position);
                    }
                });

            }

            return convertView;
        }


        class ViewHolder {
            ImageView image;
            ImageView delIv;
        }

        public void  setCallBack(callBack callBack){
            this.mCallBack = callBack;
        }



    }

    public interface  callBack{
        void action(int position);
    }

}
